import source.sharedmemory : SHMmanager;
import std.json;
import std.array;
import std.string;
import std.conv;

int main(string[] args)
{
    string[] ex = split(args[2], " ");
    ex.length = 10;
    JSONValue settings = parseJSON(args[3]);

    //     0       1      2        3      4     5
    // username PRIVMSG #blah :something else else

    // Exit command, requires access level of 0
    if (ex[3] == ":" ~ settings["opPrefix"].str ~ "kill" ||
        ex[3] == ":" ~ settings["opPrefix"].str ~ "die")
    {
        if (ex[0] in settings["access"])
        {
            if (settings["access"][ex[0]].integer == 0)
            {
                SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                ioman.appendContents(
                    JSONValue(["method": "irc", "submethod": "send", "data": "PRIVMSG " ~ ex[2] ~ " :Exiting"]).toString ~ "\n" ~
                    JSONValue(["method": "core", "submethod": "exit", "data": ""]).toString
                );
            }
        }
    }

    if (ex[3] == ":" ~ settings["opPrefix"].str ~ "help" && ex[4] == "core")
    {
        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
        string nick = ex[0][indexOf(ex[0], ":") + 1 .. indexOf(ex[0], "!")];
        ioman.appendContents(
            JSONValue(["method": "irc", "submethod": "send", "data": "PRIVMSG " ~ nick ~ " :0 - kill|die"]).toString ~ "\n" ~
            JSONValue(["method": "irc", "submethod": "send", "data": "PRIVMSG " ~ nick ~ " :1 - add|del access <<Username>> <[AccessLevel]>"]).toString ~ "\n" ~
            JSONValue(["method": "irc", "submethod": "send", "data": "PRIVMSG " ~ nick ~ " :2 - add|del channel <<ChannelName>> [[ChannelKey]]"]).toString ~ "\n" ~
            JSONValue(["method": "irc", "submethod": "send", "data": "PRIVMSG " ~ nick ~ " :2 - add|del apiKey <<KeyName>> <[Key]>"]).toString ~ "\n" ~
            JSONValue(["method": "irc", "submethod": "send", "data": "PRIVMSG " ~ nick ~ " :2 - add|del module <<ModuleName>>"]).toString ~ "\n" ~
            JSONValue(["method": "irc", "submethod": "send", "data": "PRIVMSG " ~ nick ~ " :2 - list modules"]).toString ~ "\n" ~
            JSONValue(["method": "irc", "submethod": "send", "data": "PRIVMSG " ~ nick ~ " :3 - add|del ignored <<Username>>"]).toString
        );
    }

    if (ex[3] == ":" ~ settings["opPrefix"].str ~ "add")
    {
        if (ex[0] in settings["access"])
        {
            if (settings["access"][ex[0]].integer <= 3)
            {
                // Add ignored command, access level <= 3
                if (ex[4] == "ignored")
                {
                    if (ex[5] != "")
                    {
                        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                        ioman.appendContents(
                            JSONValue(["method": "core", "submethod": "addignored", "data": ex[5]]).toString
                        );
                    }
                }
            }

            if (settings["access"][ex[0]].integer <= 2)
            {
                // Add channel command, access level <= 2
                if (ex[4] == "channel")
                {
                    if (ex[5] != "")
                    {
                        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                        ioman.appendContents(
                            JSONValue(["method": "irc", "submethod": "join", "data": "{\"chan\":\""~ex[5]~"\", \"key\":\""~ex[6]~"\"}"]).toString
                        );
                    }
                }

                // Add apiKey command, access level <= 2
                if (ex[4] == "apiKey")
                {
                    if (ex[5] != "" && ex[6] != "")
                    {
                        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                        ioman.appendContents(
                            JSONValue(["method": "core", "submethod": "addkey", "data": "{\"name\":\""~ex[5]~"\", \"key\":\""~ex[6]~"\"}"]).toString
                        );
                    }
                }

                // Add module command, access level <= 2
                if (ex[4] == "module")
                {
                    if (ex[5] != "")
                    {
                        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                        ioman.appendContents(
                            JSONValue(["method": "core", "submethod": "addmodule", "data": ex[5]]).toString
                        );
                    }
                }
            }

            if (settings["access"][ex[0]].integer <= 1)
            {
                // Add access command, access level <= 1
                if (ex[4] == "access")
                {
                    if (ex[5] != "" && ex[6] != "" && isNumeric(ex[6]) && to!int(ex[6]) <= settings["access"][ex[0]].integer)
                    {
                        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                        ioman.appendContents(
                            JSONValue(["method": "core", "submethod": "addacc", "data": "{\"user\":\""~ex[5]~"\", \"acc\":"~ex[6]~"}"]).toString
                        );
                    }
                }
            }
        }
    }

    if (ex[3] == ":" ~ settings["opPrefix"].str ~ "list")
    {
        if (ex[0] in settings["access"])
        {
            if (settings["access"][ex[0]].integer <= 2)
            {
                if (ex[4] == "modules")
                {
                    SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                    ioman.appendContents(
                        JSONValue(["method": "core", "submethod": "listmodules", "data": ex[2]]).toString
                    );
                }
            }
        }
    }

    if (ex[3] == ":" ~ settings["opPrefix"].str ~ "del")
    {
        if (ex[0] in settings["access"])
        {
            if (settings["access"][ex[0]].integer <= 3)
            {
                // Delete ignored command, access level <= 3
                if (ex[4] == "ignored")
                {
                    if (ex[5] != "")
                    {
                        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                        ioman.appendContents(JSONValue(
                            ["method": "core", "submethod": "delignored", "data": ex[5]]).toString);
                    }
                }
            }
            if (settings["access"][ex[0]].integer <= 2)
            {
                // Delete channel command, access level <= 2
                if (ex[4] == "channel")
                {
                    if (ex[5] != "")
                    {
                        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                        ioman.appendContents(JSONValue(
                            ["method": "irc", "submethod": "part",
                            "data": "{\"chan\":\""~ex[5]~"\", \"key\":\""~ex[6]~"\"}"]).toString);
                    }
                }

                // Delete apiKey command, access level <= 1
                if (ex[4] == "apiKey")
                {
                    if (ex[5] != "")
                    {
                        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                        ioman.appendContents(JSONValue(
                            ["method": "core", "submethod": "delkey", "data": ex[5]]).toString);
                    }
                }

                // Delete module command, access level <= 2
                if (ex[4] == "module")
                {
                    if (ex[5] != "")
                    {
                        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                        ioman.appendContents(JSONValue(
                            ["method": "core", "submethod": "delmodule", "data": ex[5]]).toString);
                    }
                }
            }

            if (settings["access"][ex[0]].integer <= 1)
            {
                // Delete access command, access level <= 1
                if (ex[4] == "access")
                {
                    if (ex[5] != "")
                    {
                        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
                        ioman.appendContents(JSONValue(
                            ["method": "core", "submethod": "delacc", "data": ex[5]]).toString);
                    }
                }
            }
        }
    }

    return 0;
}
